<?php
namespace app\yunda\admin;

use app\common\builder\ZBuilder;
use app\yunda\model\Member as MemberModel;
use think\Db;

/**
 * Class Category会员设置
 * @package app\yunda\admin
 */
class Member extends Base{
    public function index($config_type='member')
    {
        // 获取查询和排序规则
        $map = $this->getMap();
        $order = $this->getOrder();
        $map['delete_time']=NULL;
        $data = Db::table('dp_yd_member',true)
            // ->view('xiaoan_post',['title'],'xiaoan_post.id=xiaoan_comment.post_id')
            ->where($map)
            ->order($order)
            ->order('id desc')
            ->paginate()->each(function($item){
                $item['level']=config('member_level')[$item['level']];
                return $item;
            });

        $list_tab=$this->szMenu();
        return ZBuilder::make('table')
            ->setTabNav($list_tab,  'member')
            ->setTableName('yd_member')  //需要修改 这里修改正确可以直接修改字段 删除
            ->addColumns([
                ['id','id'],
                ['level','会员级别'],
                ['name','会员级别名称'],
                ['fee','会员费'],
                ['yh_fee','享有优惠'],
                ['create_time','创建时间','datetime'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButton('add', [],[])
            ->addRightButton('edit',[],[])
            ->addTopButtons(['delete'])
            ->setRowList($data)
            //->addFilter(['title' => 'admin_action', 'username'])
//            ->setSearch(['id' => 'ID'])
            ->fetch();
    }
    public function add(){
        if($this->request->isPost()){
            // 第一步 获取数据 如果后台面向大众 需要用only方法
            $data = request()->post();
            // 第二部 验证数据
            $result = $this->validate($data,'Member');
            if($result!==true) $this->error($result);
            // 第三部 存储
            MemberModel::create($data);
            $this->success('保存成功', null, '_parent_reload');
        }
        return ZBuilder::make('form')
            ->addFormItems([
                ['select','level','会员级别','必须',config('member_level')],
                ['text','name','会员级别名称'],
                ['number','fee','会员费','必须'],
                ['number','yh_fee','享有优惠','必须'],
            ])
            ->fetch();
    }
    public function edit($id=null){
        if($this->request->isPost()){
            $data = request()->post();
            $result = $this->validate($data,'Member');
            if($result!==true) $this->error($result);
            if($id==5)$data['config_name']=1;
            MemberModel::update($data);
            $this->success('保存成功', null, '_parent_reload');
        }
        $data = MemberModel::get($id);
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden','id'],  // 多增加一个影藏ID 即可
                ['select','level','会员级别','必须',config('member_level')],
                ['text','name','会员级别名称'],
                ['number','fee','会员费','必须'],
                ['number','yh_fee','享有优惠','必须'],
            ])
            ->setFormData($data)
            ->fetch();
    }
    //软删除
    public function delete($record = [])
    {
        $data=request()->param();
//        if(in_array(5,$data['ids']))$this->error('不能删除默认签到配置');
        Brokerage::destroy(['id'=>['in',$data['ids']]]);//删除多条
        $this->success('删除成功', null, '_parent_reload');
    }
}